import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_app/MainProvider.dart';
import 'package:provider/provider.dart';

geneReply(List<ReplyDetail> list,ScrollController controller){
  return ListView.builder(
    controller: controller,
    shrinkWrap: true,
    itemCount: list.length,
    itemBuilder: (context,index){
      return ReplyList(reply: list[index],);
    },
  );
}


geneAfterReply(List<ReplyDetail> list,ScrollController controller){
  return ListView.builder(
    controller: controller,
    shrinkWrap: true,
    itemCount: list.length,
    itemBuilder: (context,index){
      return AfterReply(reply: list[index],);
    },
  );
}



class Reply extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    double rpx = MediaQuery.of(context).size.width/750;
    MainProvider mainProvider=Provider.of(context);
    List<ReplyDetail> list=List<ReplyDetail>();
    list.add(mainProvider.reply);
    list.add(mainProvider.reply);
    list.add(mainProvider.reply);
    ScrollController scrollController = mainProvider.scrollController;
    return SingleChildScrollView(
      controller: scrollController,
      child: Container(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            child: ListTile(
              leading: Container(
                width: 10*rpx,
              ),
              trailing: GestureDetector(
                child: Icon(Icons.close,color: Colors.black,),
                onTap: (){
                  Timer(Duration(milliseconds: 100),(){
                    Navigator.pop(context);
                  });
                },
              ),
              title: Center(child: Text("10条评论"),),
            ),
          ),
          geneReply(list,mainProvider.scrollController),
        ],
      ),
    ),
    );
  }
}

class ReplyList extends StatelessWidget {
  ReplyList({Key key,this.reply}):super(key: key);
  final ReplyDetail reply;
  List<ReplyDetail> list = List<ReplyDetail>();
  @override
  Widget build(BuildContext context) {
  MainProvider mainProvider=Provider.of(context);
  ReplyDetail replay = mainProvider.reply;
    list.add(replay);
    list.add(replay);
    list.add(replay);
    print(reply.replyContent);
    double rpx = MediaQuery.of(context).size.width/750;
    return Container(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                width: 100*rpx,
                height: 100*rpx,
                padding: EdgeInsets.all(10*rpx),
                child: CircleAvatar(
                  backgroundImage: NetworkImage("${reply.replyAva}"),
                ),
              ),
              Container(
                width: 550*rpx,
                child: ListTile(
                  title: Text("${reply.makerName}",maxLines: 2,overflow: TextOverflow.ellipsis,),
                  // subtitle: Text(reply.replyContent,maxLines: 2,overflow: TextOverflow.ellipsis,),
                  subtitle: RichText(
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    text: TextSpan(text: "${reply.replyContent}",
                    style: TextStyle(color: Colors.black),
                    
                    children: [
                      TextSpan(text:"${reply.dateTime}")
                    ]),
                  ),

                ),
              ),
              Container(
                width: 100*rpx,
                child: GestureDetector(
                  child: Icon(Icons.favorite,color: Colors.grey[300],),
                  onTap: (){

                  },
                ),
              ),
            ],
          ),
          geneAfterReply(list,mainProvider.scrollController)
        ],
      ),
    );
  }
}

class AfterReply extends StatelessWidget {
  AfterReply({Key key,this.reply}):super(key: key);
  final ReplyDetail reply;
  @override
  Widget build(BuildContext context) {
    double rpx = MediaQuery.of(context).size.width/750;
    return Container(
      child: Column(
        children: [
          Row(
            children: [
              Container(
                width: 100*rpx,
              ),
              Container(
                width: 550*rpx,
                child: Row(
                  children: [
                    Container(child: CircleAvatar(backgroundImage: NetworkImage(reply.replyAva),),width: 70*rpx,height: 70*rpx,padding: EdgeInsets.all(5*rpx),),
                    Container(
                      width: 480*rpx,
                      child: ListTile(title: Text(reply.makerName),subtitle: Text(reply.replyContent),),
                    )
                  ],
                ),
              ),
              Container(
                width: 100*rpx,
                child: Icon(Icons.favorite,color: Colors.grey[300],),
              ),
            ],
          )
        ],
      ),
    );
  }
}